import React, {PropsWithChildren} from 'react'
import {EditSubGoodsRequest, SubGoodsResponse} from "../apis/GoodsApi";
import {Button, Form, Input, InputNumber, Space} from "antd";
import {MinusCircleOutlined, PlusOutlined} from "@ant-design/icons";

export interface EditSubGoodsFormItemProps extends PropsWithChildren {
    create?:boolean
}

const EditSubGoodsFormItem: React.FC<EditSubGoodsFormItemProps> = ({create}) => {
    return <>
        <Form.List name={"subGoodsList"}>

            {(fields, {add, remove}) => {
                return <>{fields.map((field,index) => {
                    return <Space>
                        <Form.Item label={`子货品${index+1}`} name={[field.name, "number"]} required
                                   rules={[{required: true, message: "请输入编号或唯一特征"}]}>
                            <Input placeholder={"请输入编号或唯一特征"}/>
                        </Form.Item>
                        <Form.Item name={[field.name, "name"]}
                                   label={" "}
                                   required={false}
                                   rules={[{required: true, message: "请输入名称"}]}>
                            <Input placeholder={"请输入名称"}/>
                        </Form.Item>
                        <Form.Item name={[field.name, "amount"]}
                                   label={" "}
                                   required={false}
                                   initialValue={0}
                                   rules={[{required: true, message: "请输入库存"}, {type: "number", min: create?0:undefined}]}>
                            <InputNumber min={0} type={"number"} placeholder={"请输入库存"}/>
                        </Form.Item>
                        <MinusCircleOutlined onClick={() => {
                            remove(field.name);
                        }}/>
                    </Space>
                })}
                    <Form.Item>
                        <Button type="dashed" onClick={() => {
                            add();
                        }} block icon={<PlusOutlined/>}>
                            添加子货品
                        </Button>
                    </Form.Item>
                </>

            }}
        </Form.List>
    </>
}

export default EditSubGoodsFormItem


